package com.tinyideatech.common.core.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tinyideatech.common.constant.Constants;
import com.tinyideatech.common.core.entity.ApiResult;
import com.tinyideatech.common.core.entity.ApiResult.Type;
import com.tinyideatech.common.core.entity.PageEntity;
import com.tinyideatech.common.core.entity.PageResult;
import com.tinyideatech.common.utils.DateUtils;
import com.tinyideatech.common.utils.ServletUtils;
import com.tinyideatech.common.utils.StringUtils;
import com.tinyideatech.common.utils.sql.SqlUtil;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.beans.PropertyEditorSupport;
import java.util.Date;
import java.util.List;

public class BaseController
{
    /**
     * 将前台传递过来的日期格式的字符串，自动转化为Date类型
     */
    @InitBinder
    public void initBinder(WebDataBinder binder)
    {
        // Date 类型转换
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport()
        {
            @Override
            public void setAsText(String text)
            {
                setValue(DateUtils.parseDate(text));
            }
        });
    }

    private PageEntity buildPageRequest()
    {
        PageEntity pageDomain = new PageEntity();
        pageDomain.setPageNum(ServletUtils.getParameterToInt(Constants.PAGE_NUM));
        pageDomain.setPageSize(ServletUtils.getParameterToInt(Constants.PAGE_SIZE));
        pageDomain.setOrderByColumn(ServletUtils.getParameter(Constants.ORDER_BY_COLUMN));
        pageDomain.setIsAsc(ServletUtils.getParameter(Constants.IS_ASC));
        return pageDomain;
    }

    /**
     * 设置请求分页数据
     */
    protected Page startPage()
    {
        PageEntity pageEntity = buildPageRequest();
        Integer pageNum = pageEntity.getPageNum();
        Integer pageSize = pageEntity.getPageSize();

        OrderItem orderItem = new OrderItem();
        if(StringUtils.isNotEmpty(pageEntity.getOrderByColumn())){
            orderItem.setColumn(pageEntity.getOrderByColumn());
            orderItem.setAsc("asc".equals(pageEntity.getIsAsc()));
        }else{
            orderItem.setColumn("create_time");
            orderItem.setAsc(false);
        }

        Page page = new Page(pageNum, pageSize);
        page.addOrder(orderItem);
        return page;
    }

    /**
     * 获取request
     */
    public HttpServletRequest getRequest()
    {
        return ServletUtils.getRequest();
    }

    /**
     * 获取response
     */
    public HttpServletResponse getResponse()
    {
        return ServletUtils.getResponse();
    }

    /**
     * 获取session
     */
    public HttpSession getSession()
    {
        return getRequest().getSession();
    }

    /**
     * 响应请求分页数据
     */
    public PageResult page(IPage page)
    {
        PageResult pageResult = new PageResult();
        pageResult.setCode(0);
        pageResult.setRows(page.getRecords());
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    /**
     * 响应返回结果
     * 
     * @param rows 影响行数
     * @return 操作结果
     */
    protected ApiResult toAjax(int rows)
    {
        return rows > 0 ? success() : error();
    }

    /**
     * 响应返回结果
     */
    protected ApiResult toAjax(boolean result)
    {
        return result ? success() : error();
    }

    /**
     * 返回成功
     */
    public ApiResult success()
    {
        return ApiResult.success();
    }

    /**
     * 返回失败消息
     */
    public ApiResult error()
    {
        return ApiResult.error();
    }

    /**
     * 返回成功消息
     */
//    public ApiResult success(String message)
//    {
//        return ApiResult.success(message);
//    }

    public ApiResult success(Object object)
    {
        return ApiResult.success(object);
    }

    public ApiResult success(Integer message)
    {
        return ApiResult.success(message);
    }

    public ApiResult success(List list)
    {
        return ApiResult.success(list);
    }

    /**
     * 返回失败消息
     */
    public ApiResult error(String message)
    {
        return ApiResult.error(message);
    }

    /**
     * 返回错误码消息
     */
    public ApiResult error(Type type, String message)
    {
        return new ApiResult(type, message);
    }

    /**
     * 页面跳转
     */
    public String redirect(String url)
    {
        return StringUtils.format("redirect:{}", url);
    }
}
